package com.aitou.mrylive.presentation.ui.room.create;

import android.util.Log;

import com.google.gson.Gson;
import com.aitou.mrylive.data.bean.BaseResponse;
import com.aitou.mrylive.data.bean.PushStreamInfo;
import com.aitou.mrylive.data.bean.ThemBean;
import com.aitou.mrylive.domain.AnchorManager;
import com.aitou.mrylive.domain.LocalDataManager;
import com.aitou.mrylive.presentation.ui.base.BaseObserver;
import com.aitou.mrylive.presentation.ui.base.BasePresenter;

import rx.Subscription;

/**
 * @author Muyangmin
 * @since 1.0.0
 */
public class CreateRoomPresenter extends BasePresenter<CreateRoomInterface> {

    private AnchorManager anchorManager;

    public CreateRoomPresenter(CreateRoomInterface uiInterface) {
        super(uiInterface);
        anchorManager = new AnchorManager();
    }

    public void generatePushStreaming(){
        String roomId = LocalDataManager.getInstance().getLoginInfo().getCurrentRoomNum();
        Subscription subscription = anchorManager.generatePushStreaming(roomId)
                .compose(this.<BaseResponse<PushStreamInfo>>applyAsySchedulers())
                .subscribe(new BaseObserver<BaseResponse<PushStreamInfo>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<PushStreamInfo> response) {
                        String origin = new Gson().toJson(response.getData());
                        getUiInterface().onPushStreamReady(origin);
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        super.onError(throwable);
                    }
                });
        addSubscription(subscription);
    }

//    得到话题Bean
    public void getThemBean(){
        Subscription subscription = anchorManager.getThemBean()
                .compose(this.<BaseResponse<ThemBean>>applyAsySchedulers())
                .subscribe(new BaseObserver<BaseResponse<ThemBean>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<ThemBean> response) {
                        Log.i("mrl",response.getData()+"成功了");
                        getUiInterface().onThemBean(response.getData());
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        super.onError(throwable);
                        Log.i("mrl","失败了");
                        getUiInterface().onThemBean(new ThemBean());
                    }
                });
        addSubscription(subscription);
    }

    //    创建房间传递标题
    public void postCreatRoom(String token,String title,String roomid,String city,String province){
        Log.i("mrl",token+title+"     "+roomid+"进来了的");
        Subscription subscription = anchorManager.postCreatRoom(token,title,roomid,city,province)
                .compose(this.<BaseResponse<Object>>applyAsySchedulers())
                .subscribe(new BaseObserver<BaseResponse<Object>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
//                        Log.i("mrl",response.getData()+"成功了");
//                        getUiInterface().getCreamRoom(response.toString());
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.i("mrl","失败了");
                        super.onError(throwable);
                    }
                });
        addSubscription(subscription);
    }

}
